#!/bin/bash
##Reproject tifs from lambert to latlong: #for all the files in a directory, reproject and save in a new folder
##Requires GDAL
##Pre-condition: Specified input directory of tif files in lambert azimuthal equal area projection (originals left unchanged)
##Post-condition: Specified output directory with new tif files reprojected into geographic (latlong) projection

#Input parameters: 
#bash scriptname.sh inputdirectory outputdirectory 


#  /data4/afsisdata/IRI_Sum_test/reproject_tifs/./reproject.sh /data4/afsisdata/IRI_Sum_test/reproject_tifs/ /data4/afsisdata/GridsForGeoserverGeogProjection

# to run in background


InputDir=$1
OutputDir=$2


#Mosaic from OutputDatasets

cd $InputDir
#ls $InputDir |grep East > DateList

#list=$(ls |grep tif)
#echo $list

#time for file in ${list[*]} 
for file in *.tif
do
#extract date

    echo $file
    eachFile=${file/\.tif/} #remove the 'tif' from the filename
    echo $eachFile
    #projecting FROM laea to longlat
    mkdir $OutputDir/$eachFile
    time gdalwarp -overwrite -s_srs '+proj=laea +lat_0=5 +lon_0=20 +x_0=0 +y_0=0 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs' -t_srs '+proj=longlat +datum=WGS84 +no_defs' $InputDir/$eachFile'.tif' $OutputDir/$eachFile'.tif' -multi -wm 5000

    chmod -R 775 $OutputDir/$eachFile 

#time gdalwarp -overwrite -s_srs '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs' -t_srs '+proj=laea +lat_0=5 +lon_0=20 +x_0=0 +y_0=0 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs' "$OutputDir"/"$baseDir"mosaic"$BandName" "$OutputDir"/"$BaseDir"mosaic"$BandName"LAEA -multi -wm 5000
done



